package scu.maqiang.homogenization;

import scu.maqiang.fes.BVPType;
import scu.maqiang.fes.FES3T42;
import scu.maqiang.fes.Materials;
import scu.maqiang.mesh.MatrixFunc;
import scu.maqiang.mesh.Mesh3T4;
import scu.maqiang.mesh.ScalarFunc;
import scu.maqiang.numeric.MVO;

public class ElasticitySOTS3DT4 {

	public static void main(String[] args) {
		long begin = System.nanoTime();
	    int N = 8;
	    double eps= 1.0 / N;
	    Mesh3T4 cellMesh = new Mesh3T4().cube(32, 32, 32);
	    ScalarFunc regionCellFunc = (xyz, llable, param) -> {
            return (Math.abs(xyz[0] - 0.5) < 0.25 && Math.abs(xyz[1] - 0.5) < 0.25 && Math.abs(xyz[2] - 0.5) < 0.25)? 1.0:-1.0;
        };
	    cellMesh.setDomainLabel(regionCellFunc, null, 10);
	    cellMesh.saveMesh_mesh("CellMeshCube.mesh");
	    FES3T42 fsCell = new FES3T42(cellMesh);
	    double[][] FOCS = new double[6][fsCell.GetNdof()];
		double[][] SOCS = new double[18][fsCell.GetNdof()];
		double[][] D = new double[6][6];
		SOTS3DT4102 ss = new SOTS3DT4102(fsCell, null);
		
		//ScalarCoefFunc[] gcf = {cf};
		
		double E1 = 4.137e11, E2 = 4.137e9;
		double Nu1 = 0.2, Nu2 = 0.35;
		double[][] D1 = Materials.ElasticConstituteMatrix(E1, Nu1, BVPType.COMMON);
		System.out.println(MVO.toString(D1));
		double[][] D2 = Materials.ElasticConstituteMatrix(E2, Nu2, BVPType.COMMON);
		System.out.println(MVO.toString(D2));
		MatrixFunc mf = (xyz, label, param) -> label == 10? D1 : D2;
		ss.elasticityCellSolution(mf, null, BVPType.CONSTITUTE_MATRIX_COMMON, FOCS, D, SOCS);
	    
		for(int i = 0; i < 6; i++) {
			System.out.println("min: " + MVO.min(FOCS[i]) + "\tmax: " + MVO.max(FOCS[i]));
		}
		System.out.println("Homogenized Elasticity: ");
		System.out.println(MVO.toString(D));
		for(int i = 0; i < 18; i++) {
			System.out.println("min: " + MVO.min(SOCS[i]) + "\tmax: " + MVO.max(SOCS[i]));
		}
		//cellMesh.toTecplot("CellFOCS.dat", FOCS);
		//cellMesh.toTecplot("CellSOCS.dat", SOCS);
		long end = System.nanoTime();
		System.out.println("Solve time: " + (end - begin) / 1.0e9 + " s");
	}

}
